package com.zenithmind.article.pojo.domain;

import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

import jakarta.validation.constraints.NotBlank;
import java.time.LocalDateTime;

/**
 * 文章标签关联实体
 * 遵循单一职责原则：专注于文章和标签的关联关系
 * 遵循封装原则：通过方法控制关联状态
 * 遵循值对象模式：表示文章与标签的多对多关系
 */
@EqualsAndHashCode(callSuper = true)
@Getter
@NoArgsConstructor
@Schema(name = "文章标签关联实体", description = "文章标签关联关系实体")
public class ArticleTag extends BaseEntity {

    @NotBlank(message = "文章ID不能为空")
    @Schema(description = "文章ID")
    private String articleId;

    @NotBlank(message = "标签ID不能为空")
    @Schema(description = "标签ID")
    private String tagId;

    @Schema(description = "关联状态")
    private AssociationStatus status = AssociationStatus.ACTIVE;

    @Schema(description = "关联时间")
    private LocalDateTime associationTime;

    @Schema(description = "标签权重")
    private Integer weight = 1;

    /**
     * 关联状态枚举
     */
    public enum AssociationStatus {
        ACTIVE(1, "有效"),
        INACTIVE(0, "无效");

        private final Integer code;
        private final String description;

        AssociationStatus(Integer code, String description) {
            this.code = code;
            this.description = description;
        }

        public Integer getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static AssociationStatus fromCode(Integer code) {
            if (code == null) return ACTIVE;
            for (AssociationStatus status : values()) {
                if (status.code.equals(code)) {
                    return status;
                }
            }
            return ACTIVE;
        }
    }

    // === 构造方法 ===

    /**
     * 创建文章标签关联
     */
    public ArticleTag(String articleId, String tagId) {
        this.articleId = articleId;
        this.tagId = tagId;
        this.status = AssociationStatus.ACTIVE;
        this.associationTime = LocalDateTime.now();
        this.weight = 1;
    }

    /**
     * 创建带权重的文章标签关联
     */
    public ArticleTag(String articleId, String tagId, Integer weight) {
        this(articleId, tagId);
        this.weight = weight != null && weight > 0 ? weight : 1;
    }

    // === 业务方法 ===

    /**
     * 激活关联
     */
    public void activate() {
        this.status = AssociationStatus.ACTIVE;
        this.associationTime = LocalDateTime.now();
    }

    /**
     * 停用关联
     */
    public void deactivate() {
        this.status = AssociationStatus.INACTIVE;
    }

    /**
     * 设置权重
     */
    public void setWeight(Integer weight) {
        this.weight = weight != null && weight > 0 ? weight : 1;
    }

    /**
     * 检查关联是否有效
     */
    public boolean isActive() {
        return this.status == AssociationStatus.ACTIVE;
    }

    /**
     * 检查关联是否无效
     */
    public boolean isInactive() {
        return this.status == AssociationStatus.INACTIVE;
    }

    /**
     * 检查是否为高权重标签
     */
    public boolean isHighWeight() {
        return this.weight >= 5;
    }

    /**
     * 获取状态码（用于数据库存储）
     */
    public Integer getStatusCode() {
        return this.status.getCode();
    }

    /**
     * 设置状态（用于数据库读取）
     */
    public void setStatusCode(Integer statusCode) {
        this.status = AssociationStatus.fromCode(statusCode);
    }

    /**
     * 创建业务标识
     * 用于缓存键和唯一性约束
     */
    public String getBusinessKey() {
        return this.articleId + ":" + this.tagId;
    }

    /**
     * 比较权重
     */
    public int compareWeight(ArticleTag other) {
        if (other == null) return 1;
        return Integer.compare(this.weight, other.weight);
    }
}
